import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { Stage } from './entities/stage.entity';
import { RedisService } from 'src/modules/common/services/redis.service';

@Injectable()
export class StageCacheService {
  constructor(private readonly redisService: RedisService) {}

  @OnEvent('stage.update')
  async handleStageUpdate(payload: any) {
    await this.deleteStage(payload.id);
  }
  async getStage(stageId: number) {
    const res = await this.redisService.get(`stage:${stageId}`);
    return res ? JSON.parse(res) : null;
  }
  async setStage(stage: Stage) {
    await this.redisService.set(`stage:${stage.id}`, JSON.stringify(stage));
  }
  async deleteStage(stageId: number) {
    await this.redisService.delete(`stage:${stageId}`);
  }
}
